/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.rate.point.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springblade.common.enums.rate.RoleTypeEnum;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.rate.point.excel.PointRecordExcel;
import org.springblade.modules.rate.point.vo.PointRankVO;
import org.springblade.modules.rate.point.vo.PointStatVO;
import org.springblade.modules.rate.point.vo.req.PointRankReqVO;
import org.springblade.modules.rate.point.vo.req.PointStatReqVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.excel.UserExcel;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List;
import java.util.Map;

import org.springblade.modules.rate.point.entity.PointRecord;
import org.springblade.modules.rate.point.vo.PointRecordVO;
import org.springblade.modules.rate.point.service.IPointRecordService;
import org.springblade.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 积分记录表 控制器
 *
 * @author Blade
 * @since 2021-09-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("rate/point/pointrecord")
@Api(value = "积分记录表", tags = "积分记录表接口")
public class PointRecordController extends BladeController {

	private IPointRecordService pointRecordService;

	/**
	 * 积分情况
	 */
	@GetMapping("/stat")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "【小程序】积分情况", notes = "传入pointRecord")
	public R<PointStatVO> detail(PointStatReqVO req) {
		PointStatVO detail = pointRecordService.stat(req);
		return R.data(detail);
	}
	/**
	 * 排行榜
	 */
	@GetMapping("/rank")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "【小程序】排行榜", notes = "传入pointRecord")
	public R<List<PointRankVO>> rank(PointRankReqVO req) {
		List<PointRankVO> items = pointRecordService.rank(req );
		return R.data(items);
	}
	/**
	 * 我的排行榜
	 */
	@GetMapping("/myRank")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "【小程序】我的排行榜，单行", notes = "传入pointRecord")
	public R<PointRankVO> myRank(PointRankReqVO req) {

		PointRankVO rankVO = pointRecordService.myRank(req );
		return R.data(rankVO);
	}
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "详情", notes = "传入pointRecord")
	public R<PointRecord> detail(PointRecord pointRecord) {
		PointRecord detail = pointRecordService.getOne(Condition.getQueryWrapper(pointRecord));
		return R.data(detail);
	}

	/**
	 * 分页 积分记录表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表查询", notes = "传入pointRecord")
	public R<IPage<PointRecord>> list(PointRecord pointRecord, Query query) {
		IPage<PointRecord> pages = pointRecordService.page(Condition.getPage(query), Condition.getQueryWrapper(pointRecord).orderByAsc("update_time"));
		return R.data(pages);
	}

	/**
	 * 自定义分页 积分记录表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入pointRecord")
	public R<IPage<PointRecordVO>> page(PointRecordVO pointRecord, Query query) {
		query.setDescs("update_time");
		IPage<PointRecordVO> pages = pointRecordService.selectPointRecordPage(Condition.getPage(query), pointRecord);
		return R.data(pages);
	}

	/**
	 * 新增 积分记录表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入pointRecord")
	public R save(@Valid @RequestBody PointRecord pointRecord) {
		return R.status(pointRecordService.save(pointRecord));
	}

	/**
	 * 修改 积分记录表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入pointRecord")
	public R update(@Valid @RequestBody PointRecord pointRecord) {
		return R.status(pointRecordService.updateById(pointRecord));
	}

	/**
	 * 新增或修改 积分记录表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入pointRecord")
	public R submit(@Valid @RequestBody PointRecord pointRecord) {
		return R.status(pointRecordService.saveOrUpdate(pointRecord));
	}


	/**
	 * 删除 积分记录表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(pointRecordService.removeByIds(Func.toLongList(ids)));
	}

	    /**
     * 数组 代码自定义代号
     */
    @GetMapping("/item")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "全部列表", notes = "传入pointRecord")
    public R<List<PointRecord>> list(PointRecord pointRecord) {
        List<PointRecord> items = pointRecordService.list(Condition.getQueryWrapper(pointRecord));
        return R.data(items);
    }
	/**
	 * 导出用户
	 */
	@GetMapping("export")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "导出用户", notes = "传入user")
	public void exportUser(@ApiIgnore @RequestParam Map<String, Object> pointRecord, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<PointRecord> queryWrapper = Condition.getQueryWrapper(pointRecord, PointRecord.class);
		if (!AuthUtil.isAdministrator()) {
			queryWrapper.lambda().eq(PointRecord::getTenantId, bladeUser.getTenantId());
		}
		queryWrapper.lambda().eq(PointRecord::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<PointRecordExcel> list = pointRecordService.export(queryWrapper);
		ExcelUtil.export(response, "积分数据" + DateUtil.time(), "积分数据", list, PointRecordExcel.class);
	}



}
